#encoding: utf-8

module UI
  # Class that show the icon sprite of a Pokemon
  class PokemonIconSprite2 < SpriteSheet
    # Create a new animated pokemon icon
    # @param viewport [Viewport]
    def initialize(viewport)
      super(viewport, 2, 1)
      @counter = 0
      @max_counter = 60
    end

    # Set the pokemon
    # @param pokemon [PFM::Pokemon, nil]
    def data=(pokemon)
      if(self.visible = (pokemon ? true : false))
        bmp = self.bitmap = pokemon.icon
        self.set_origin(width / 2, height / 2)
        @max_counter = max_counter(pokemon)
      end
    end
    
    # Update the pokemon animation
    def update
      @counter += 1
      if @counter >= @max_counter
        self.sx = (@sx + 1) % 2
        @counter = 0
      end
    end

    private

    # Find the max number of frame before switching the @sx value
    # @param pokemon [PFM::Pokemon, nil]
    def max_counter(pokemon)
      return Float::INFINITY if pokemon.asleep? || pokemon.dead?
      #Changes speed for Pokémon with status effects
      return 15 if pokemon.status != 0
      #Changes speed for Pokémon
      return 10 + ((1 - pokemon.hp_rate) * 60).to_i
    end
  end
end